; (function ($) {
  const Accordion = function (element, options) {
    this.$element = $(element)
    this.options = $.extend({}, Accordion.DEFAULTS, options)
    this.$parent = this.$element.closest(this.options.parent)
    this.$panel = this.$element.parent().next('.panel-content')
  }

  Accordion.DEFAULTS = {
    toggle: true,
    parent: '.panel-group'
  }

  Accordion.prototype.isToggle = function () {
    return this.options.toggle
  }
  
  Accordion.prototype.hide = function () {
    this.$element.removeClass('active')
    this.$panel.removeClass('expand')
  }

  Accordion.prototype.show = function () {
    this.$element.addClass('active')
    this.$panel.addClass('expand')
    this.process()

  }

  Accordion.prototype.process = function () {
    if( this.isToggle() ){
      this.$parent.find('[data-plugin="accordion"]').not(this.$element).removeClass('active')
      this.$parent.find('>.panel>.panel-content').not(this.$panel).removeClass('expand')
      $('body, html').animate({scrollTop: this.$parent.offset().top}, 100)
    }else{
      $('body, html').animate({scrollTop: this.$panel.offset().top}, 100)
    }
  }

  Accordion.prototype.init = function () {
    this[this.$element.hasClass('active') ? 'hide' : 'show']()
  }

  $.fn.accordion = function (options) {
    let accordion = new Accordion(this, options)
    return this.each(function () {
      accordion.init()
    })
  }

  $(document).ready(function () {
    $('[data-plugin="accordion"]').on('click', function (e) {
      e.preventDefault()
      $(this).accordion()
    })
  })
})(jQuery)